package com.example.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.backend.common.ResultMessage;
import com.example.backend.entity.QiShou;
import com.example.backend.service.impl.QiShouService;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j;
import org.springframework.web.bind.annotation.*;

@Log4j
@RestController
@RequestMapping("/qishou")
public class QiShouController {
    @Resource
    private QiShouService qiShouService;

    @PostMapping
    public ResultMessage addQiShou(@RequestBody QiShou qiShou){
        boolean flag = qiShouService.addQiShou(qiShou);
        if (flag){
            return ResultMessage.success(true,"添加成功");
        }
        return ResultMessage.error("添加失败");
    }

    @DeleteMapping("/{qsid}")
    public ResultMessage deleteQiShou(@PathVariable("qsid") Integer qsid){
        boolean flag = qiShouService.deleteQiShou(qsid);
        if (flag){
            return ResultMessage.success(true,"删除成功");
        }
        return ResultMessage.error("删除失败");
    }

    @PutMapping
    public ResultMessage updateQiShou(@RequestBody QiShou qiShou){
        boolean flag = qiShouService.updateQiShou(qiShou);
        if (flag){
            return ResultMessage.success(true,"更新成功");
        }
        return ResultMessage.error("更新失败");
    }

    @GetMapping
    public ResultMessage selectQiShouByCondition(QiShou qiShou,
                                                @RequestParam(defaultValue = "1") Integer currentPage,
                                                @RequestParam(defaultValue = "5") Integer pageSize,
                                                 @RequestParam(required = false) String sortField,
                                                 @RequestParam(required = false) String sortOrder){
        IPage<QiShou> qiShouList = qiShouService.selectQiShouByCondition(qiShou,currentPage,pageSize,sortField,sortOrder);
        if (qiShouList != null){
            return ResultMessage.success(qiShouList,"查询成功");
        }
        return ResultMessage.error("查询失败");
    }

    @GetMapping("/yhid/{yhid}")
    public ResultMessage selectQiShouByYhid(@PathVariable("yhid") Integer yhid){
        System.err.println(yhid);
        QiShou qiShou = qiShouService.selectQiShouByYhid(yhid);
        if (qiShou != null){
            return ResultMessage.success(qiShou,"查询成功");
        }
        return ResultMessage.error("查询失败");
    }
}
